package com.ubb.damate.webservice;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.ubb.damate.model.Project;
import com.ubb.damate.model.Role;
import com.ubb.damate.model.User;
import com.ubb.damate.model.Workspace;
import com.ubb.damate.service.ProjectServiceLocal;
import com.ubb.damate.service.UserServiceLocal;
import com.ubb.damate.service.WorkspaceServiceLocal;
/*
import com.ubb.damate.model.EclipseUsageModel;
import com.ubb.damate.service.EclipseUsageServiceLocal;
import com.ubb.damate.model.ProjectHandlingModel;
import com.ubb.damate.model.Project;
import com.ubb.damate.service.ProjectHandlingServiceLocal;
import java.util.List;
import java.util.Map;
import java.util.Iterator;*/

/** Session Bean implementation class UserWebService */
@Stateless
@WebService()
public class UserWebService {

	@EJB(beanName = "UserService")
	private UserServiceLocal		userService;
	@EJB(beanName = "WorkspaceService")
	private WorkspaceServiceLocal	workspaceService;
	@EJB(beanName = "ProjectService")
	private ProjectServiceLocal		projectService;
/*
	@EJB(beanName = "EclipseUsageService")
	private EclipseUsageServiceLocal ecl;
	@EJB(beanName = "ProjectHandlingService")
	private ProjectHandlingServiceLocal phl;*/

	@WebMethod(action = "createOrUpdateUser", operationName = "createOrUpdateUser")
	public boolean createOrUpdateUser(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "roleId") int roleId) {
		if (!username.equalsIgnoreCase("")) {
			User user = new User();
			user.setUsername(username);

			if (!password.equalsIgnoreCase("")) {
				user.setPassword(password);
			}

			if (roleId != 0) {
				Role role = new Role();
				role.setRoleId(roleId);
				role.setRoleName("admin");
				user.setRole(role);
			}

			userService.createOrUpdate(user);

			return true;
		}

		return false;

		/*int i;
		String a = "";
		
		Map<User, EclipseUsageModel> m = ecl.getStats();
		Iterator it2 = m.entrySet().iterator();
		while(it2.hasNext()) {
			Map.Entry pairs = ((Map.Entry)it2.next());
			a += ((User)pairs.getKey()).getUsername() + " ";
			EclipseUsageModel e = (EclipseUsageModel)pairs.getValue();
			List<Integer> openTime = e.getOpenSeconds();
			List<Integer> changes = e.getChanges();
			List<Integer> projects = e.getProjects();
			for (i = 0; i < 12; i++)
				a += openTime.get(i) + " ";
			a += "      ";
			for (i = 0; i < 12; i++)
				a += changes.get(i) + " ";
			a += "      ";
			for (i = 0; i < 12; i++)
				a += projects.get(i) + " ";
			a += "      ";
			a += "      ";
		}
		
		Map<User, ProjectHandlingModel> m = phl.getStats();
		Iterator it2 = m.entrySet().iterator();
		while(it2.hasNext()) {
			Map.Entry pairs = ((Map.Entry)it2.next());
			a += ((User)pairs.getKey()).getUsername() + " ";
			ProjectHandlingModel p = (ProjectHandlingModel)pairs.getValue();
			Map<Project, List<Integer>> map = p.getNrOfFiles();
			Iterator it = map.entrySet().iterator();
			while(it.hasNext()) {
				Map.Entry pairs2 = ((Map.Entry)it.next());
				a += ((Project)pairs2.getKey()).getId().getProjectName() + " ";
				for (i = 0; i < 12; i++)
					a += ((List<Integer>)pairs2.getValue()).get(i) + " ";
				a += "      ";
			}
			a += "      ";
			a += "      ";
		}
		
		return a;*/
	}

	@WebMethod(action = "allUser", operationName = "allUser")
	public List<User> allUser() {
		return userService.allUser();
	}

	@WebMethod(action = "login", operationName = "login")
	public boolean login(@WebParam(name = "username") String username, @WebParam(name = "password") String password) {
		return userService.login(username, password);
	}

	@WebMethod(action = "workspacesForUser", operationName = "workspacesForUser")
	public List<Workspace> workspacesForUser(@WebParam(name = "username") String username) {
		return workspaceService.find(username);
	}

	@WebMethod(action = "projectsForUser", operationName = "projectsForUser")
	public List<Project> projectsForUser(@WebParam(name = "username") String username) {
		return projectService.find(username);
	}
}